package org.jtao.gbsubject.service;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import net.logstash.logback.encoder.org.apache.commons.lang.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.jtao.gbsubject.domain.College;
import org.jtao.gbsubject.repository.CollegeRepository;
import org.jtao.gbsubject.service.dto.CollegeDTO;
import org.jtao.gbsubject.service.mapper.CollegeMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;


/**
 * Service Implementation for managing College.
 */
@Service
@Transactional
public class CollegeService {

    private final Logger log = LoggerFactory.getLogger(CollegeService.class);

    private final CollegeRepository collegeRepository;

    private final CollegeMapper collegeMapper;

    public CollegeService(CollegeRepository collegeRepository, CollegeMapper collegeMapper) {
        this.collegeRepository = collegeRepository;
        this.collegeMapper = collegeMapper;
    }

    /**
     * Save a college.
     *
     * @param collegeDTO the entity to save
     * @return the persisted entity
     */
    public CollegeDTO save(CollegeDTO collegeDTO) {
        log.debug("Request to save College : {}", collegeDTO);
        College college = collegeMapper.toEntity(collegeDTO);
        college = collegeRepository.save(college);
        return collegeMapper.toDto(college);
    }

    /**
     * Get all the colleges.
     *
     * @param pageable the pagination information
     * @return the list of entities
     */
    @Transactional(readOnly = true)
    public Page<CollegeDTO> findAll(Pageable pageable) {
        log.debug("Request to get all Colleges");
        return collegeRepository.findAll(pageable)
            .map(collegeMapper::toDto);
    }

    /**
     * Get one college by id.
     *
     * @param id the id of the entity
     * @return the entity
     */
    @Transactional(readOnly = true)
    public CollegeDTO findOne(Long id) {
        log.debug("Request to get College : {}", id);
        College college = collegeRepository.findOne(id);
        System.out.println("************************" + college.toString());
        return collegeMapper.toDto(college);
    }

    /**
     * Delete the college by id.
     *
     * @param id the id of the entity
     */
    public void delete(Long id) {
        log.debug("Request to delete College : {}", id);
        collegeRepository.delete(id);
    }

    public Integer importColleges(MultipartFile file) {
        List<College> collegeList = ImportService.importExcel(file, 1, 1, College.class);
        int sum = 0;
        for (int i = 0; i < collegeList.size(); i++) {
            College college = collegeList.get(i);
            try {
                if (college.getName() != null) {
                    College college1 = collegeRepository.findOneByName(college.getName());
                    if (college1 == null) {
                        sum++;
                        this.save(collegeMapper.toDto(college));
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        System.out.println("导入数据一共【" + sum + "】行");
        return sum;
    }

}
